package com.leyou.page.mq;

import com.leyou.common.constants.MQConstants;
import com.leyou.page.service.PageService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.common.constants.MQConstants.Exchange.*;
import static com.leyou.common.constants.MQConstants.Queue.*;
import static com.leyou.common.constants.MQConstants.RoutingKey.*;

/**
 * @package com.leyou.page.mq
 * @description: 监听商品商品上下架修改静态化的页面
 * @author: 许超越
 * @date 2019/7/5 15:32
 * @version: V1.0
 */
@Component
public class ItemListener {

    @Autowired
    private PageService pageService;


    /**
     * 监听商品上架
     * @param spuId
     */
    @RabbitListener(bindings = @QueueBinding(
                                            value = @Queue(name = PAGE_ITEM_UP, durable = "true"),
                                            exchange = @Exchange(name = ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
                                            key = ITEM_UP_KEY))
    public void insertListen(Long spuId) {
        if (spuId != null) {
            //新增静态页面
            pageService.createItemHtml(spuId);
        }
    }


    /**
     * 监听商品下架
     * @param spuId
     */
    @RabbitListener(bindings = @QueueBinding(
                                            value = @Queue(name = PAGE_ITEM_DOWN, durable = "true"),
                                            exchange = @Exchange(name = ITEM_EXCHANGE_NAME, type = ExchangeTypes.TOPIC),
                                            key = ITEM_DOWN_KEY))
    public void deleteListen(Long spuId) {
        if (spuId != null) {
            //删除静态页面
            pageService.deleteHtml(spuId);
        }
    }
}
